
$NG.AllReady(function (page, { useAction, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    console.log('detail Ready');
    var mstform = $NG.getCmpApi('p_form_tbsqsq_m');
    console.log(mstform);
    console.log(mstform.getItem('u_xmmc').getValue());

    const saveBtn = ['save', 'check']
    saveBtn.forEach(item => {
        useBeforeClick(async function ({ args }) {
            let tbsj = mstform.getItem('u_tbsj').getValue();
            let bill_dt = mstform.getItem('bill_dt').getValue(); // 开票日期

            // 检查两个日期是否有效
            if (!tbsj || !bill_dt) {
                return;
            }
            const tbsjDate = new Date(tbsj);
            const billDtDate = new Date(bill_dt);
            const tbsjDay = new Date(tbsjDate.getFullYear(), tbsjDate.getMonth(), tbsjDate.getDate());
            const billDtDay = new Date(billDtDate.getFullYear(), billDtDate.getMonth(), billDtDate.getDate());

            if (billDtDay > tbsjDay) {
                $NG.alert('申请日期晚于投标时间两天禁止保存')
                return false;
            }

        }, item);
    })

    useValuesChange(()=>{
        hideHeadKeys()
    },"u_bltbsqlx")

    // 隐藏
    function hideHeadKeys() {
        const phid = (Number(mstform.getItem("u_bltbsqlx").getValue()) || 0)
        const ka = ['u_xmmc','u_bdmc','u_tbsj','u_sfgsnbry','u_bsqr','u_bsqrdw','u_bsqrlxfs','phid_fill_psn','bill_dt','u_bz','u_bsqr1','u_bsqrdw1','u_bsqrlxfs1','u_bsqrsfzhm']
        const k1 = ['u_xmmc','u_bdmc','u_tbsj','u_sfgsnbry','u_bsqr','u_bsqrdw','u_bsqrlxfs','phid_fill_psn','bill_dt','u_bz',]
        const k2 = ['u_xmmc','u_bdmc','u_tbsj','phid_fill_psn','bill_dt','u_bz',]
        const k3 = ['u_xmmc','u_bdmc','u_tbsj','u_sfgsnbry','u_bsqr1','u_bsqrdw1','u_bsqrlxfs1','u_bsqrsfzhm','phid_fill_psn','bill_dt','u_bz',]
        let i = []
        if([6870000000001054,6870000000001055,6870000000001056].includes(phid)){
            mstform.getItem("u_sfgsnbry").setValue('01')
            i = k1
        } else if(phid == 6870000000001057){
            i = k2
        } else if(phid == 6870000000001058){
            i = k3
            mstform.getItem("u_sfgsnbry").setValue('02')
        }
        i.forEach(el=>{
            mstform.getItem(el).setProps({
                hidden:false
            })
        })
        ka.forEach(key=>{
            if(!i.includes(key)){
                mstform.getItem(key).setProps({
                    hidden:true
                })
            }
        })
    }

    try {
        csh()
    } catch (e) { }
    function csh() {
        if (mstform.getItem('u_sfxybltbsq').getValue() == '01') {
            mstform.getItem('u_sfgsnbry').setValue('01')
            $NG.updateUI((updater) => {
                const fields = ["u_bsqr", "u_bsqrdw", "u_bsqrlxfs"];
                fields.forEach((field) => {
                    updater.fieldSetForm.p_form_tbsqsq_m[field].setProps({
                        hidden: false, //隐藏
                        required: true, //必输
                        // disabled: true, //只读
                    });
                });
            });
            $NG.updateUI((updater) => {
                const fields = ["u_sfgsnbry"];
                fields.forEach((field) => {
                    updater.fieldSetForm.p_form_tbsqsq_m[field].setProps({
                        hidden: false, //隐藏
                        required: true, //必输
                        // disabled: true, //只读
                    });
                });
            });
            $NG.updateUI((updater) => {
                const fields = ["u_wxbltbsqdyy"];
                fields.forEach((field) => {
                    updater.fieldSetForm.p_form_tbsqsq_m[field].setProps({
                        hidden: true, //隐藏
                        required: false, //必输
                        // disabled: true, //只读
                    });
                });
            });
        } else {
            mstform.getItem('u_sfgsnbry').setValue('')
            $NG.updateUI((updater) => {
                const fields = ["u_bsqr", "u_bsqrdw", "u_bsqrlxfs"];
                fields.forEach((field) => {
                    updater.fieldSetForm.p_form_tbsqsq_m[field].setProps({
                        hidden: true, //隐藏
                        required: false, //必输
                        // disabled: true, //只读
                    });
                });
            });
            $NG.updateUI((updater) => {
                const fields = ["u_sfgsnbry"];
                fields.forEach((field) => {
                    updater.fieldSetForm.p_form_tbsqsq_m[field].setProps({
                        hidden: true, //隐藏
                        required: false, //必输
                        // disabled: true, //只读
                    });
                });
            });
            $NG.updateUI((updater) => {
                const fields = ["u_wxbltbsqdyy"];
                fields.forEach((field) => {
                    updater.fieldSetForm.p_form_tbsqsq_m[field].setProps({
                        hidden: false, //隐藏
                        required: true, //必输
                        // disabled: true, //只读
                    });
                });
            });
        }
    }
    useValuesChange(({ args }) => {
        let u_bsqr = mstform.getItem('u_bsqr').getValue()
        console.log(mstform.getItem('u_bsqr').getValue());
        $NG.execServer("searchhr", { phid: u_bsqr }, (res) => {
            if (res.count == 0) return
            const data = JSON.parse(res.data)[0].extendObjects;
            console.log(data);
            mstform.getItem('u_bsqrlxfs').setValue(data.mobile1)
            // mstform.getItem('u_tbsj').setValue(data.user_tbrq)
            $NG.updateState((updater) => {
                updater.data.p_form_tbsqsq_m.setProps({
                    u_bsqrdw: {
                        value: data.phid_org,
                        label: data.oname
                    }
                })
            })
        });
    }, "p_form_tbsqsq_m.u_bsqr");
    useValuesChange(({ args }) => {
        console.log(mstform.getItem('u_bsqrdw').getValue());

    }, "p_form_tbsqsq_m.u_bsqrdw");
    useValuesChange(({ args }) => {
        console.log(mstform.getItem('u_sfxybltbsq').getValue());
        csh()
        noBl()
        // console.log(args, "u_ywlb");
    }, "p_form_tbsqsq_m.u_sfxybltbsq");
    useValuesChange(({ args }) => {
        let u_bdmc = mstform.getItem('u_bdmc').getValue()
        console.log(mstform.getItem('u_bdmc').getValue());
        $NG.execServer("searchNew", { user_bdmc: u_bdmc }, (res) => {
            if (res.count == 0) return
            const data = JSON.parse(res.data)[0].extendObjects;
            console.log(data);

            mstform.getItem('u_xmmc').setValue(data.project_name)
            mstform.getItem('u_tbsj').setValue(data.user_tbsj)
            // $NG.updateState((updater) => {
            //     updater.formValue.setProps({
            //         PhidApplyPsn: {
            //             value: data.phid,
            //             label: data.bill_name
            //         }
            //     })
            // })
        });
    }, "p_form_tbsqsq_m.u_bdmc");



    // 判断无需办理投标授权的原因
    try {
        noBl()
    } catch (e) { }
    function noBl() {
        const u_sfxybltbsq = mstform.getItem("u_sfxybltbsq").getValue()
        let lock = false
        if ((u_sfxybltbsq && u_sfxybltbsq == '01') || !u_sfxybltbsq) {
            lock = true
        }
        $NG.updateUI((updater) => {
            updater.fieldSetForm.p_form_tbsqsq_m.u_wxbltbsqdyy.setProps({
                hidden: lock,
                required: !lock,
            })
        })

        $NG.updateUI((updater) => {
            updater.fieldSetForm.p_form_tbsqsq_m.u_bz.setProps({
                hidden: !lock,
            })
        })
    }

}, function () {
    console.log('list Ready');
});

